package com.lh.service;

import com.lh.dao.UserMapper;
import com.lh.entity.UsianUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper um;


    public boolean login(UsianUser user) {


        UsianUser usianUser = new UsianUser();

        usianUser.setUserName(user.getUserName());

        List<UsianUser> list = um.select(usianUser);

        if (list.isEmpty()) {
            return false;
        } else {
            UsianUser success = list.get(0);
            //加密 判断密码
            String password = user.getPassword();

            String str = "woaijiyun";

            if (password.equals(success.getPassword().replace(str, ""))) {
                return true;
            } else {
                return false;
            }
        }


    }

    public void regist(UsianUser user) {
        String str = "woaijiyun";
        StringBuffer password = new StringBuffer(user.getPassword());
        user.setPassword(password.insert(3, str).toString());
        um.insert(user);
    }

    public void updateUser(UsianUser user) {
        String str = "woaijiyun";
        StringBuffer password = new StringBuffer(user.getPassword());
        user.setPassword(password.insert(3, str).toString());

        UsianUser user2 = um.findUserByName(user.getUserName());

        user.setId(user2.getId());

        um.updateByPrimaryKey(user);
    }
}
